package data;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import domain.Contrato;
import domain.Jugador;

public class JDBCContratoDAO implements IContratoDAO{
	
	private Connection conn;
	private IJugadorDAO IJDao = new JDBCJugadorDAO();
	
	public JDBCContratoDAO (){
		conn = ConnectionManager.getInstance().checkOut();
	}
	
	protected void finalize() {
        ConnectionManager.getInstance().checkIn(conn);
    }
	
	
	public void insertContrato(Jugador jugador,Contrato contrato, String oid) {
		String sql = "INSERT INTO contratos (oid,oid_jugador,fin_contrato,clausula" +
				",cedido_sfc,cedido_fuera) VALUES (?,?,?,?,?,?)";
		PreparedStatement stmt = null;
		
		try{
			stmt = conn.prepareStatement(sql);
			
			stmt.setString(1, oid);
			stmt.setString(2, IJDao.obtnerOID(jugador.getNombre()));
			stmt.setString(3, contrato.getFin_contrato());
			stmt.setString(4, contrato.getClausula());
			stmt.setBoolean(5, contrato.isCedido_sfc());
			stmt.setBoolean(6, contrato.isCedido_fuera());
			
			stmt.executeUpdate();
		
		} catch (SQLException e) {
		    System.out.println("Message: " + e.getMessage());
		    System.out.println("SQLState: " + e.getSQLState());
		    System.out.println("ErrorCode: " + e.getErrorCode());
		} finally {
		    try {
		        if (stmt != null) {
		            stmt.close();
		        }
		    } catch (SQLException e) {
		    }
		}
		
	}
	
	public void updateContrato (String id,String fecha_contrato){
		String sql = "UPDATE contratos SET fin_contrato = ? WHERE oid_jugador = ?";
		
		PreparedStatement stmt = null;

		try {
			stmt = conn.prepareStatement(sql);
			
			stmt.setString(1, fecha_contrato);
			stmt.setString(2, id);
			
			stmt.executeUpdate();
		}
	catch (SQLException e) {
		
		System.out.println("Message: " + e.getMessage());
		System.out.println("SQLState: " + e.getSQLState());
		System.out.println("ErrorCode: " + e.getErrorCode());
	} finally {
		try {
			if (stmt != null) {
				stmt.close();
			}
		} catch (SQLException e) {
		}
	}
		
	}
	
	public void updateClausula (String id,String clausula){
		String sql = "UPDATE contratos SET clausula = ? WHERE oid_jugador = ?";
		
		PreparedStatement stmt = null;

		try {
			stmt = conn.prepareStatement(sql);
			
			stmt.setString(1, clausula);
			stmt.setString(2, id);
			
			stmt.executeUpdate();
		}
	catch (SQLException e) {
		
		System.out.println("Message: " + e.getMessage());
		System.out.println("SQLState: " + e.getSQLState());
		System.out.println("ErrorCode: " + e.getErrorCode());
	} finally {
		try {
			if (stmt != null) {
				stmt.close();
			}
		} catch (SQLException e) {
			}
		}
		
	}
	
	public String obtnerOID(String oid) {
		String res = "";
		String sql = "SELECT oid FROM contratos WHERE oid_jugador =?";
		PreparedStatement stmt = null;
		ResultSet result = null;

		try {
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, oid);

			result = stmt.executeQuery();

			if (result.next()) {
				res = result.getString(1);
			}
		} catch (SQLException e) {
			System.out.println("Message: " + e.getMessage());
			System.out.println("SQLState: " + e.getSQLState());
			System.out.println("ErrorCode: " + e.getErrorCode());

		} finally {
			try {
				if (result != null)
					result.close();
				if (stmt != null)
					stmt.close();
			} catch (SQLException e) {
			}
		}
		return res;	
	}

}
